import { nanoid } from "nanoid";



export const Func = {

	getFieldName: (row) => {
		if (!row.fieldType) {
			return;
		}
		let result = "";
		for (const fieldType of fieldTypeList.value) {
			if (fieldType.code == row.fieldType) {
				result = fieldType.name;
			}
		}
		return result;
	},

	repeatDoit: (list, row) => {
		if (!list) {
			return null;
		}
		let b = null;
		list.forEach((item) => {
			if (item._key === row._key) {
				b = item;
			} else {
				if (item.children && item.children.length > 0) {
					const cb = Func.repeatDoit(item.children, row);
					if (cb) {
						b = cb;
					}
				}
			}
		});
		return b;
	},

	repeatListDoit: (list, row) => {
		if (!list) {
			return null;
		}
		let b = null;
		list.forEach((item) => {
			if (item._key === row._key) {
				b = list;
			} else {
				if (item.children && item.children.length) {
					b = Func.repeatListDoit(item.children, row);
				}
			}
		});
		return b;
	},

	clearHiLight: (list) => {
		if (!list) {
			return null;
		}
		let b = null;
		list.forEach((item) => {
			item.hiLightFlag = false;
			if (item.children && item.children.length) {
				b = Func.clearHiLight(item.children);
			}
		});
		return b;
	},

	repairJson: (newValue) => {
		if (!newValue.params) {
			newValue.params = [];
		}
		if (!(newValue.params instanceof Array)) {
			newValue.params = [newValue.params];
		}
		if (newValue.basicTypeFlag == null) {
			newValue.basicTypeFlag = false;
		}
		if (newValue.isArrayFlag == null) {
			newValue.isArrayFlag = false;
		}
		if (newValue.jsonEditorFlag == null) {
			newValue.jsonEditorFlag = false;
		}
		if (newValue.isUpdateColumnFlag == null) {
			newValue.isUpdateColumnFlag = false;
		}
		if (newValue.isAddColumnFlag == null) {
			newValue.isAddColumnFlag = false;
		}


		newValue.basicTypeFlag = Func.strBoolToBool(newValue.basicTypeFlag);
		newValue.isArrayFlag = Func.strBoolToBool(newValue.isArrayFlag);

		Func.repairJsonFor(newValue.params);

		const rootPath = "$";
		const rowb = Func.repeatCompPath(newValue.params, {}, rootPath, rootPath);

		return newValue;
	},

	repairJsonFor: (newValue) => {
		for (const f of newValue) {
			if (f._key == null) {
				f._key = nanoid(5);
			}
			if (f.editorFlag == null) {
				f.editorFlag = false;
			}
			if (f.hiLightFlag == null) {
				f.hiLightFlag = false;
			}
			if (f.isArrayFlag == null) {
				f.isArrayFlag = false;
			}
			if (f._showChildren == null) {
				f._showChildren = true;
			}

			if (f.defaultValue == null) {
				f.defaultValue = null;
			}

			if (f.children != null && !Array.isArray(f.children)) {
				f.children = [f.children];
			}
			if (f.children != null && f.children.length > 0) {
				Func.repairJsonFor(f.children);
			}

			f.editorFlag = Func.strBoolToBool(f.editorFlag);
			f.hiLightFlag = Func.strBoolToBool(f.hiLightFlag);
			f.isArrayFlag = Func.strBoolToBool(f.isArrayFlag);
			f._showChildren = Func.strBoolToBool(f._showChildren);
		}
	},

	strBoolToBool: (boolStr) => {
		if (boolStr && boolStr == "false") {
			boolStr = false;
		}
		if (boolStr && boolStr == "true") {
			boolStr = true;
		}
		return boolStr;
	},
	repeatCompPath: (list, row, path, jsonPath) => {
		if (!list) {
			return null;
		}
		let b = null;
		list.forEach((item) => {
			item.path = path + "." + item.fieldCode;

			item.jsonPath = jsonPath + "." + item.fieldCode;
			if (item.isArrayFlag) {
				item.jsonPath = item.jsonPath + "[*]";
			}
			if (item._key === row._key) {
				b = item;
			} else {
				if (item.children && item.children.length) {
					const cb = Func.repeatCompPath(item.children, row, item.path, item.jsonPath);
					if (cb) {
						b = cb;
					}
				}
			}
		});
		return b;
	},


	findRowByPath: (list, path) => {
		if (!list) {
			return null;
		}
		let b = null;
		list.forEach((item) => {
			if (item.path === path) {
				b = item;
			} else {
				if (item.children && item.children.length) {
					const cb = Func.findRowByPath(item.children, path);
					if (cb) {
						b = cb;
					}
				}
			}
		});
		return b;
	},

	jsonToParamDefine: (d) => {
		const de = []
		//把元素展开
		for (const c in d) {
			let cb = d[c]
			const id = nanoid(5)
			const tpdata = {
				_key: id,
				fieldCode: c,
				fieldName: c,
				fieldType: 'String',
				fieldDesc: c,
				children: [],
				editorFlag: false,
				_showChildren: true,
				isArrayFlag: false
			}
			if (cb instanceof Array) {
				tpdata.isArrayFlag = true
				cb = cb[0]
			}

			if (Func.isObject(cb)) {
				tpdata.fieldType = 'Object'
				tpdata.children = Func.jsonToParamDefine(cb)
			}
			de.push(tpdata)
		}
		return de
	},


	isObject: (value) => {
		return value instanceof Object
	}

}
